package gepes.dao;

import gepes.exepcion.DAOExcepcion;
import gepes.modelo.Profesor;
import gepes.util.ConexionBD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import gepes.exepcion.LoginExcepcion;

public class ProfesorDAO extends BaseDAO {
	// 
	public int existe(String campo, String dato) throws DAOExcepcion {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {					
			String query = "select count(0) from profesor where "
					+ campo + "=?"; 		
			
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, dato);
			rs = stmt.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return 0;

	}
	/*public int login(Profesor us) throws DAOExcepcion{
		String query = "select count(0) from profesor where "
				+ " correo=? and clave=?";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, us.getCorreo());
			stmt.setString(2, us.getClave());
			rs = stmt.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}		
		return 0;
	}*/
	
	public Profesor validar(String correo, String clave) throws DAOExcepcion, LoginExcepcion{
		String query = "select count(0) from profesor "
				+ "where correo=? and clave=?";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Profesor vo = new Profesor();
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, correo);
			stmt.setString(2, clave);
			rs = stmt.executeQuery();

			if (rs.next()) {
				vo.setCorreo(rs.getString("correo"));
				vo.setClave(rs.getString("clave"));
							
			} else {
				throw new LoginExcepcion("No existe");
			}
		} catch (LoginExcepcion e) {
			System.err.println(e.getMessage());
			throw new LoginExcepcion(e.getMessage());
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	
	public Profesor insertar(Profesor pr) throws DAOExcepcion {
		String query = "insert into profesor(nombres,apepaterno,apematerno,sexo,tipodocumento,nrodocumento,correo,celular,"
				+ "clave,perfil,foto) values(?,?,?,?,?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, pr.getNombres());
			stmt.setString(2, pr.getApepaterno());
			stmt.setString(3, pr.getApematerno());
			stmt.setString(4, pr.getSexo());
			stmt.setString(5, pr.getTipodocumento());
			stmt.setString(6, pr.getNrodocumento());
			stmt.setString(7, pr.getCorreo());
			stmt.setString(8, pr.getCelular());
			stmt.setString(9, pr.getClave());
			stmt.setString(10, pr.getPerfil());
			stmt.setString(11, pr.getFoto());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("Error insertar");
			}

			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			pr.setCodprofesor(id);

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return pr;
	}

	public Profesor actualizar(Profesor pr) throws DAOExcepcion {
		String query = "update profesor set nombres=?, apepaterno=? where codprofesor=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, pr.getNombres());
			stmt.setString(2, pr.getApepaterno());
			stmt.setInt(3, pr.getCodprofesor());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return pr;
	}

}
